<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="inventoryPlan-body" v-else>
		<!--  顶部  -->
		<top-tab :options="stockList" disabledStock :locationList='LocationList' :locationName="locationName"
			@stockChange="stockChange" @handleBarnChoose="handleBarnChoose"></top-tab>
		<!-- 全部、已扫箱数 -->
		<view :style="{height: inventoryType === 'review' ? '106px' : '70px'}">
			<view class="boxCount">
				<view class="boxCount_item">
					<view class="boxCount_item_title">全部箱数：</view>
					<view class="">{{ boxCount.sumBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item">
					<view class="boxCount_item_title">扫描箱数：</view>
					<view class="">{{ boxCount.scanBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item">
					<view class="boxCount_item_title">未扫箱数：</view>
					<view class="">{{ boxCount.notSweptBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item">
					<view class="boxCount_item_title">仓位箱数：</view>
					<view class="">{{ boxCount.locationSumBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item">
					<view class="boxCount_item_title">仓位已扫数：</view>
					<view class="">{{ boxCount.locationScanBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item">
					<view class="boxCount_item_title">仓位未扫数：</view>
					<view class="">{{ boxCount.locationNotSweptBoxCount || 0 }}</view>
				</view>
				<view class="boxCount_item" v-if="inventoryType === 'review'">
					<view class="boxCount_item_title">并集未扫数：</view>
					<view class="">{{ boxCount.notSweepSum || 0 }}</view>
				</view>
			</view>
		</view>
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item :left-options="options" @click="handleDelete(item)" v-for="item in purchaseList">
					<view class="panel" @click="openDetail(item)">
						<view class="panel_title">
							<view class="panel_title_left">{{ item.materialName }}</view>
						</view>
						<view class="panel_content">
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">批次：</view>
									<view class="panel_content_row_item_value">{{ item.batchNo }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">仓位：</view>
									<view class="panel_content_row_item_value">{{ item.locationNo }}</view>
								</view>
							</view>
							<view class="panel_content_row" v-if="type==='1'">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">账面箱数：</view>
									<view class="panel_content_row_item_value">{{ item.bookBoxCount }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">账面数：</view>
									<view class="panel_content_row_item_value">
										{{ Number((item.bookCount || 0).toFixed(6)) }}
									</view>
								</view>
							</view>
						</view>
						<view class="panel_dashboard">
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number success">{{ item.boxCount || 0 }}</view>
								<view class="panel_dashboard_item_value">箱数</view>
							</view>
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number default">
									{{ Number((item.count || 0).toFixed(6)) }}
								</view>
								<view class="panel_dashboard_item_value">总重</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleWarehousing" class="footerBtn save"><i
						class="iconfont icon-duihao btn_icon"></i>完成
				</button>
				<button @click="handleScanCode" class="footerBtn scan" type="primary"><i
						class="iconfont icon-saomiao btn_icon"></i>扫码
				</button>
			</view>
		</view>

		<!--  操作面板  -->
		<min-action-sheet ref="as"></min-action-sheet>

		<!-- 弹出层 -->
		<MaterialPopup ref="scaningPopup" title="物料盘点" type="inventoryPlan" @success="initData"></MaterialPopup>
		<!-- <BarnPopup ref="barnPopup" @confirm="onLocationChange"></BarnPopup> -->
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import minActionSheet from '@/components/min-action-sheet/min-action-sheet'
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import MaterialPopup from "@/components/popup/materialPopup"
	import BarnPopup from "@/components/popup/barnPopup"

	export default {
		components: {
			topTab,
			minActionSheet,
			uniLoading,
			uniEmptyInfo,
			MaterialPopup,
			BarnPopup
		},
		data() {
			return {
				type: null, //1明盘 2盲盘
				inventoryType: null,
				billNo: null,
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				LocationList: [],
				stockNo: '',
				locationNo: '',
				locationName: '',
				boxCount: {},
			}
		},
		async onLoad(e) {
			this.billNo = e.billNo;
			this.stockNo = e.stockNo;
			this.type = e.type;
			this.inventoryType = e.inventoryType;
			this.getBoxCount();
			await this.getStockList();
			await this.getLocationList();
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			let self = this
			uni.$on('deleteItem', ({
				material,
				close
			}) => {
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success(res) {
						if (res.confirm) {
							const {
								boxNo
							} = material;
							self.$api.deleteUserTaskDetailBox({
								billNo: self.billNo,
								boxNo
							}, {
								loading: true
							}).then((res) => {
								if (!res.res) return;
								self.initData();
								close && close();
							});
						}
					}
				})
			})
		},
		methods: {
			/** 查询仓库*/
			async getStockList() {
				this.loading = true;
				try {
					let {
						res,
						stockList = []
					} = await this.$api.getCustomerStock();
					if (!res) return;
					this.stockList = stockList.filter(item => item.stockNo === this.stockNo);
				} finally {
					this.loading = false;
				}
			},
			/** 查询仓位*/
			async getLocationList() {
				this.loading = true;
				try {
					let {
						res,
						data = []
					} = await this.$api.getLocationList({
						stockNo: this.stockNo
					});
					if (!res) return;
					this.LocationList = data;
				} finally {
					this.loading = false;
				}
			},
			
			/**
			 * 查询总箱数和已扫数
			 */
			getBoxCount() {
				let self = this
				self.$api.getBoxCount({
					billNo: self.billNo,
					locationNo: self.locationNo
				}).then(res => {
					if (res.res) {
						self.boxCount = res
					}
				})
			},
			/** 初始化数据 */
			initData() {
				if (!this.stockNo) return;
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.purchaseList = []
				this.getBoxCount()
				this.getPurchaseList()
			},

			/**
			 *
			 */
			async openDetail(material) {
				const {
					key
				} = material
				let {
					res,
					data = []
				} = await this.$api.getUserTaskDetailBox({
					billNo: this.billNo,
					key
				}, {
					loading: true
				});
				if (!res) return;
				let dataList = data.map(item => {
					return {
						billNo: this.billNo,
						materialName: item.materialName,
						materialNo: item.materialNo,
						batchNo: item.batchNo,
						boxNo: item.boxNo,
						count: item.count,
						materialSpec: item.materialSpec,
						locationNo: item.locationNo,
						bookCount: item.bookCount,
						status: item.status,
						type: this.type
					}
				})
				console.log(dataList)
				this.openPopNVue('盘点明细', dataList, 'inventoryPlan')
			},

			/** 获取数据 */
			async getPurchaseList() {
				let {
					res,
					data = []
				} = await this.$api.getUserTaskDetail({
					billNo: this.billNo,
					stockNo: this.stockNo,
					locationNo: this.locationNo
				}, {
					loading: true
				});
				if (!res) return;
				this.purchaseList = data;
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				uni.stopPullDownRefresh();
			},

			/** 完成 */
			handleWarehousing() {
				const {
					billNo,
					stockNo,
					locationNo,
					purchaseList
				} = this;
				if (!stockNo) {
					uni.showToast({
						title: '请选择仓库',
						icon: 'none'
					});
					return;
				}
				if (!locationNo) {
					uni.showToast({
						title: '请选择仓位',
						icon: 'none'
					});
					return;
				}
				if (!purchaseList || !purchaseList.length) {
					uni.showToast({
						title: '没有物料',
						icon: 'none'
					});
					return;
				}
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否完成？',
					success(res) {
						if (res.confirm) {
							self.$api.saveUserTask({
								billNo,
								stockNo,
								locationNo
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '已完成',
										icon: 'none'
									})
									// self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				this.initData()
			},

			/**
			 * 选择仓位
			 */
			handleBarnChoose(e) {
				this.locationNo = JSON.parse(e).locationNo;
				this.locationName = JSON.parse(e).locationName;
				this.initData()
			},

			/** 扫码 */
			async handleScanCode() {
				this.$refs.scaningPopup.open(this.stockNo, this.locationNo, this.billNo)
			},

			/**
			 * 删除物料
			 * @param {Object} e
			 */
			handleDelete(material) {
				const {
					key
				} = material
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否删除物料？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteUserTaskDetail({
								key,
								billNo: self.billNo
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '物料删除成功',
										icon: 'none'
									})
									self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			onReachBottom() {
				if (!this.pageConfig.bottomEnd) {
					this.pageConfig.pageIndex++
					this.pageConfig.bottomLoading = true
					this.getPurchaseList();
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";

	.inventoryPlan-body {
		::v-deep​​.topTab .tabTab_body {
			box-shadow: none !important;
		}
	}

	.boxCount {
		position: fixed;
		/* #ifdef H5 */
		top: 94px;
		/* #endif */
		/* #ifndef H5 */
		top: 50px;
		/* #endif */
		left: 0px;
		width: 100%;
		font-size: 14px;
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		background-color: #fff;
		border-top: 1px solid #F2F6FC;
		box-shadow: 0 3px 5px #eee;
		z-index: 99;

		.boxCount_item {
			min-width: 33.3%;
			height: 35px;
			display: flex;
			align-items: center;
			justify-content: center;

			.boxCount_item_title {
				color: $font-hb;
			}
		}
	}
</style>
